package main.randoms;

import java.util.ArrayList;
import java.util.HashMap;

import main.bots.Methods;
import main.bots.ScriptManager;

public class RandomHandler implements Runnable {
	
	public ArrayList<AntiRandom> randomsList = new ArrayList<AntiRandom>();
	static Thread thread;
	private static RandomHandler rH;

	@Override
	public void run() {
		System.out.println("RandomHandler thread started");
		Thread.currentThread().setName("RandomHandlerTHREAD");
		setUpRandoms();
		processRandoms();

	}
	public static RandomHandler getRandomHandler() {
		if(rH == null) {
			System.out.println("Setting RandomHandler up..");
			rH = new RandomHandler();
			thread = new Thread(rH);
			thread.start();	
		}
		return rH;
	}
	private void processRandoms() {
		while(Methods.getClientToUse().scriptMan.botRunning == null) {
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			
		}
		while(Methods.getClientToUse().scriptMan.botRunning != null) {
			for(AntiRandom ar : randomsList) {
				if(ar == null) continue; //Not sure how but ok
				if(ar.shouldActivate()) {
					Methods.getClientToUse().scriptMan.pauseCurrentScript();
					int x;
					while((x = ar.loop()) > 0) {
						try {
							Thread.sleep(x);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					Methods.getClientToUse().scriptMan.pauseCurrentScript();
				
				}
			}
		}
		processRandoms(); //Gayness. Script ended so lets wait for new one
		
	}

	private void setUpRandoms() {
		// TODO Auto-generated method stub
		
	}

}
